package com.actimize.webapp.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.actimize.model.Account;
import com.actimize.model.Payee;
import com.actimize.model.Transaction;
import com.actimize.service.AccountManager;
import com.actimize.webapp.action.BaseAction;
import com.opensymphony.xwork2.Preparable;



public class AccountAction extends BaseAction implements Preparable  {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public String getGeneralInfo() throws IOException {
		HttpServletRequest request = getRequest();
		
		
		Account account = accountManager.getAccount(16470560l);// getAccounts(user.getId());
		request.setAttribute("accounts16470560", account.getBalance());
		account = accountManager.getAccount(16751460l);// getAccounts(user.getId());
		request.setAttribute("accounts16751460", account.getBalance());

		Cookie[] cookie =  request.getCookies();
		System.out.println("num of cookie="+cookie.length);
		
		if (cookie != null){
			for(int i = 1; i< cookie.length;i++){
				System.out.println("Cookie["+i+"] = "+cookie[i].getName());
				if(cookie[i].getName().equals("User")){
					request.setAttribute("loggedUser", cookie[i].getValue());
					System.out.println("evo ga u kurac");
				}
		}
		}
		return SUCCESS;
	}
	
	public String quickTransfer() throws IOException{
		
		HttpServletRequest request = getRequest();
		
		if (request.getParameter("frmMakeTransfer:amount")!= null && request.getParameter("frmMakeTransfer:strSelectedRemitterAccount1")!= null
				&& request.getParameter("frmMakeTransfer:strSelectBeneficiaryAccounts1") !=null )
		{
		Double amount = new Double(request.getParameter("frmMakeTransfer:amount"));
		String remitter = new String(request.getParameter("frmMakeTransfer:strSelectedRemitterAccount1"));
		String beneficiary = new String(request.getParameter("frmMakeTransfer:strSelectBeneficiaryAccounts1"));
		
		
		//AccountManager accMng = new AccountManagerImpl();
		
		
		System.out.println("amount="+request.getParameter("frmMakeTransfer:amount"));
		System.out.println("from="+request.getParameter("frmMakeTransfer:strSelectedRemitterAccount1"));
		System.out.println("to="+request.getParameter("frmMakeTransfer:strSelectBeneficiaryAccounts1"));
		
		accountManager.quickTranfer((remitter.subSequence(remitter.indexOf("||")+2, remitter.indexOf("||", remitter.indexOf("||")+1))).toString(), 
				(beneficiary.subSequence(beneficiary.indexOf("||")+2, beneficiary.indexOf("||", beneficiary.indexOf("||")+1))).toString(),
				amount);
		

		request.setAttribute("loggedUser", getSession().getAttribute("loggedUser"));
		
		Account account = accountManager.getAccount(16470560l);// getAccounts(user.getId());
		request.setAttribute("accounts16470560", account.getBalance());
		account = accountManager.getAccount(16751460l);// getAccounts(user.getId());
		request.setAttribute("accounts16751460", account.getBalance());

		Transaction tran = new Transaction();
		
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date date = new Date();
		tran.setTranDate(dateFormat.format(date));
		
		if((remitter.subSequence(remitter.indexOf("||")+2, remitter.indexOf("||", remitter.indexOf("||")+1))).toString().equals("16470560")){
			tran.setTranDesc("Transfer to Saving Account");
			tran.setTranOut(amount);
			tran.setTranType("TRF");
			tran.setTranIn(0.0);
		}else{
			tran.setTranDesc("Transfer from Saving Account");
			tran.setTranOut(0.0);
			tran.setTranType("TRF");
			tran.setTranIn(amount);
		}
		transactionManagerTmp.saveTran(tran);
		
		Cookie[] cookie =  request.getCookies();
		if (cookie != null){
			for(int i = 1; i< cookie.length;i++)
				if(cookie[i].getName().equals("userName"))
					request.setAttribute("loggedUser", cookie[i].getValue());
		}
		}
		return SUCCESS;
	}
	
    public String logmein() throws IOException{

    	String ipAddress = getRequest().getParameter("ipAddress");
    	
    	String userName = getRequest().getParameter("j_username");
    	
    	 Cookie userNameCk = new Cookie("User",userName);
		 getResponse().addCookie(userNameCk);
		 
		 Cookie hcCk = new Cookie("UserHashCode",Integer.toString(userName.hashCode()));
		 getResponse().addCookie(hcCk);

    	 return SUCCESS;
    }
	
	public String setTransfer() throws IOException{
		HttpServletRequest request = getRequest();
		request.setAttribute("loggedUser", getSession().getAttribute("loggedUser"));
		Account account = accountManager.getAccount(16470560l);// getAccounts(user.getId());
		request.setAttribute("accounts16470560", account.getBalance());
		account = accountManager.getAccount(16751460l);// getAccounts(user.getId());
		request.setAttribute("accounts16751460", account.getBalance());
		
		return SUCCESS;
	}
	
	public String domesticPaymentInit() throws IOException{
		HttpServletRequest request = getRequest();
		Account account = accountManager.getAccount(16470560l);// getAccounts(user.getId());
		request.setAttribute("accounts16470560", account.getBalance());

		Cookie[] cookie =  request.getCookies();
		if (cookie != null){
			for(int i = 1; i< cookie.length;i++)
				if(cookie[i].getName().equals("userName"))
					request.setAttribute("loggedUser", cookie[i].getValue());
		}
		List<Payee> payeeList = payeeManager.getPayees();
		System.out.println("Read =" + payeeList.size());
		for(int i=0; i<payeeList.size(); i++)
			System.out.println(payeeList.get(i).toString());
		
		request.setAttribute("payeeList", payeeList);
		return SUCCESS;
	}
	
	public String getTransaction() throws IOException{
		//transactionManager.getAll();
		
		return SUCCESS;
	}
	
	public String domesticPayment() throws IOException{
		HttpServletRequest request = getRequest();

		
		
		if (request.getParameter("frmMakePayment:Amount")!= null && request.getParameter("frmMakePayment:benLessTwentyFive:slctPayee")!= null)
		{
			Double amount = new Double(request.getParameter("frmMakePayment:Amount"));
			String remitter = new String("16470560");
			String beneficiary = new String(request.getParameter("frmMakePayment:benLessTwentyFive:slctPayee"));
			
			/*Transaction tran = new Transaction();
			tran.setTranDesc("Payeeing to person ")
			*/accountManager.updateAmount(remitter, -amount);
			
			Transaction tran = new Transaction();
			
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			Date date = new Date();
			tran.setTranDate(dateFormat.format(date));
			System.out.println("Benef is "+beneficiary);
			Payee benef = payeeManager.getPayee(beneficiary);

			tran.setTranDesc("Payment to "+benef.getPayeeName()+"("+benef.getPayeeSort()+", "+benef.getPayeeAccount()+")");
			tran.setTranOut(amount);
			tran.setTranType("DBT");
			tran.setTranIn(0.0);
			
			transactionManagerTmp.saveTran(tran);
			
		}

		
		
		Account account = accountManager.getAccount(16470560l);// getAccounts(user.getId());
		request.setAttribute("accounts16470560", account.getBalance());


		
		Cookie[] cookie =  request.getCookies();
		if (cookie != null){
			for(int i = 1; i< cookie.length;i++)
				if(cookie[i].getName().equals("userName"))
					request.setAttribute("loggedUser", cookie[i].getValue());
		}

		return SUCCESS;
	}
	public void prepare() throws Exception {
		
			
	}

}
